﻿/*
			       ___ _      _   ___ _  _    ___  _  _ 
			      | __| |    /_\ / __| || |  / _ \| \| |
			      | _|| |__ / _ \\__ \ __ | | (_) | .` |
			      |_| |____/_/ \_\___/_||_|  \___/|_|\_|
			                                            
			  _____ _  _   _ _____   ___ ___ _____ ___ _  _ 
			 |_   _| || | /_\_   _| | _ )_ _|_   _/ __| || |
			   | | | __ |/ _ \| |   | _ \| |  | || (__| __ |
			   |_| |_||_/_/ \_\_|   |___/___| |_| \___|_||_|
			        F L A S H  O N  T H A T  B I T C H
			        
		+--------------------------------------------------------+
		|	This file is delivered as is. Any modifications to   |
		|   this class and/or sub classes may cause errors and   |
		|   can cause you compilations to fail. FOTB takes no	 |
		|   responsibility for any complications caused by the	 |
		|   following code or any modified versions of it.		 |
		+--------------------------------------------------------+

*/

package com.flashonthatbitch.filesystem {

/*
		 ___ __  __ ___  ___  ___ _____ 
		|_ _|  \/  | _ \/ _ \| _ \_   _|
		 | || |\/| |  _/ (_) |   / | |  
		|___|_|  |_|_|  \___/|_|_\ |_|  
		I M P O R T
*/
	
	import flash.desktop.Icon;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	

	/**
	 * class: IconObject
	 * package: com.flashonthatbitch.filesystem
	 * 
	 * @link http://www.stopp.se/
	 * @link http://www.flashfantastic.com/
	 * @author Carl Calderon
	 */
	public class IconObject extends Icon {
		
/*
		  ___ ___  _  _ ___ _____ ___ _   _  ___ _____ ___  ___ 
		 / __/ _ \| \| / __|_   _| _ \ | | |/ __|_   _/ _ \| _ \
		| (_| (_) | .` \__ \ | | |   / |_| | (__  | || (_) |   /
		 \___\___/|_|\_|___/ |_| |_|_\\___/ \___| |_| \___/|_|_\
		C O N S T R U C T O R
*/
		
		public function IconObject(icon:Icon=null) {
			super();
			if(icon != null)
				bitmaps = icon.bitmaps;
		}
	
/*		
		 ___ _   _ ___ _    ___ ___ 
		| _ \ | | | _ ) |  |_ _/ __|
		|  _/ |_| | _ \ |__ | | (__ 
		|_|  \___/|___/____|___\___|
		P U B L I C
*/
		
		/**
		 * Retrieves an icon in the specified size.
		 * If no icon matches the requested size and
		 * the forceMatch argument is set to 'true',
		 * the function will search for the closest
		 * match and rescale it to fit the request.
		 * 
		 * @param	size		Required size
		 * @return	An icon
		 */
		public function getIcon(size:uint = 16, forceMatch=true):BitmapData {
			if (hasIcons) {
				var closest:BitmapData = bitmaps[0];
				for (var i:int = 0; i < bitmaps.length; i++) {
					if (bitmaps[i] == size)
						return bitmaps[i];
					else	if (bitmaps[i].width - size < closest.width) {
							closest = bitmaps[0];
						}
				}
				if(forceMatch){
					var ret:BitmapData = new BitmapData(size, size, true, 0x00FFFFFF);
					var mtx:Matrix = new Matrix();
					mtx.scale(size / closest.width, size / closest.height);
					ret.draw(closest, mtx,null,null,null,true);
					mtx = null;
					closest = null;
					return ret;
				}
			}else
				throw getError("No icons found.");
			return null;
		}
		
/*
		  ___ ___ _____   ___ ___ _____ 
		 / __| __|_   _| / __| __|_   _|
		| (_ | _|  | |   \__ \ _|  | |  
		 \___|___| |_|   |___/___| |_|  
		G E T  S E T
*/
		
		/**
		 * Has icons
		 */
		public function get hasIcons():Boolean {
			if (bitmaps != null);
				return bitmaps.length > 0;
			return false;
		}
		
		/**
		 * [internal-use]
		 * Returns a pre-defined error with the specified message.
		 *  
		 * @param message		Message
		 * @return 			IconObjectError object
		 * 
		 */
		private function getError(message:String):Error {
			var err:Error = new Error(message);
			err.name = "IconObjectError";
			return err;
		}
		
	}

}